<?php

namespace app\common\model;

use app\common\service\App;
use Monolog\Handler\IFTTTHandler;
use think\Db;
use think\Model;

class SkillApply extends Model
{

    protected static $_empty = [
        'apply_id' => 0,
        'skill_name' => '',
        'explain' => ''
    ];

    public function getList($where = []){
        return $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($where)
            ->field('a.*,s.skill_name,s.img as skill_img,u.nick_name,u.header_img')
            ->order('a.apply_id desc')
            ->paginate('',false,['query'=>request()->param()]);
    }

    public function getDetail($where = [])
    {
        $data =  $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')

            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($where)
            ->field('a.*,s.skill_name,s.img as skill_img,u.nick_name,u.header_img')
            ->find();
        $map['a.status']   = 1;
        $map['a.skill_id'] = $data['skill_id'];
        $map['a.user_id']  = $data['user_id'];
        $forms = Db::name('skill_form_user')
            ->alias('a')
            ->join('skill_form f','f.form_id = a.form_id','LEFT')
            ->where($map)
            ->field('a.*,f.form_name,f.gift_id')
            ->select();
        foreach ($forms as $k => $v){
            $map2['status'] = 1;
            $map2['gift_id'] = ['in',$v['gift_id']];
            $forms[$k]['gifts'] = Db::name('gift')->where($map2)->order('price')->field('price,gift_id,gift_name')->select();
        }
        $data['forms'] = $forms;
        return $data;
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 技能详情页
     *
     */
    public function detail($map = [])
    {

        $field = 'u.sex,e.level,a.my_grade,a.apply_id,a.skill_id,a.user_id,a.img,a.voice,a.video,a.video_img,a.explain,s.skill_name,u.nick_name,u.header_img,e.online_time,u.birthday,online_status';

        $data = $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->where($map)
            ->field($field)
            ->find();

        //技能详情页第一个数组=>技能图片
       $skillImg['type'] = 'img';
       $skillImg['img']  = $data['img'];
       $skillImg['src']  = '';
       $items[] = $skillImg;

        //技能详情页第二个数组=>技能视频
        if ($data['video']){
            $video['type'] = 'video';
            $video['img']  = $data['video_img'];
            $video['src']  = $data['video'];
            $items[] = $video;
        }

        //技能详情页第三个数组=>动态视频
        $map1['status']  = 1;
        $map1['user_id'] = $data['user_id'];
        $vod = Db::name('vod')->where($map1)->field('pid,play_url,play_num,img')->cache(3)->select();
        if ($vod){
            foreach ($vod as $k => $v){
                $array['type'] = 'video';
                $array['img']  = $v['img'];
                $array['src']  = $v['play_url'];
                $items[] = $array;
            }
        }


        if ($data['online_status'] == 1){
            $data['status'] = '当前在线';
        }else{
            $data['status'] = formatTime($data['online_time']);
        }
        $data['age'] = getAge($data['birthday']);

//
//        $form['a.skill_id'] = $data['skill_id'];
//        $form['a.user_id']  = $data['user_id'];
//        $form['a.status']   = 1;
//        $form['s.status']   = 1;
//
//        $data['forms'] = Db::name('skill_form_user')->alias('a')
//            ->join('skill_form s','s.form_id = a.form_id','LEFT')
//            ->where($form)
//            ->order('a.num desc')
//            ->field('a.num,s.form_name')
//            ->select();
//
//        $tag['a.skill_id'] = $data['skill_id'];
//        $tag['a.user_id']  = $data['user_id'];
//        $tag['a.status']   = 1;
//        $tag['t.status']   = 1;
//
//        $data['tags'] = Db::name('skill_tag_user')->alias('a')
//            ->join('skill_tag t','t.tag = a.tag','LEFT')
//            ->where($tag)
//            ->order('a.num desc')
//            ->field('a.num,t.tag_name')
//            ->select();

//        $comment['a.to_user']  = $data['user_id'];
//        $comment['a.skill_id'] = $data['skill_id'];
//        $comment['a.status']   = 5;
//
//        $comments = Db::name('order')->alias('a')
//            ->join('users u','u.user_id = a.user_id','LEFT')
//            ->join('gift g','g.gift_id = a.gift_id','LEFT')
//            ->where($comment)
//            ->field('a.score,g.gift_name,g.thumbnail,g.img,a.num,a.tags,a.content,a.update_time,u.nick_name,u.header_img,u.user_id')
//            ->select();
//
//        foreach ($comments as $k => $v){
//
//            $tags['tag']    = ['in',$v['tags']];
//            $tags['status'] = 1;
//            $comments[$k]['tags'] = Db::name('skill_tag')->where($tags)->field('tag_name')->select();
//            $comments[$k]['user_id'] = hashid($v['user_id']);
//        }
//
//        $data['comments'] = $comments;

        $img['user_id'] = $data['user_id'];
        $img['status']  = 1;
        $img['type']    = 1;

        $data['imgs'] = Db::name('user_img')->where($img)->field('img')->select();

        //技能详情页第四个数组=>动态图
        if ($data['imgs']){
            foreach ($data['imgs'] as $k => $v){
                $array['type'] = 'img';
                $array['src']  = '';
                $array['img']  = $v['img'];
                $items[] = $array;
            }

        }

        $data['items'] = $items;
        $data['user_id'] = hashid($data['user_id']);

        return $data;

    }


    public function getMy($map = [])
    {
        return $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id  = a.user_id','LEFT')
            ->where($map)
            ->cache(3)
            ->order('a.num desc')
            ->field('s.skill_name,s.img,s.skill_id,a.is_use,a.num,a.apply_id,a.status,a.remark')
            ->select();
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 获取资质数据 显示资质允许赠送的礼物和形式或我允许赠送的礼物和形式
     * param bool $my 为true时获取我接受礼物和形式
     */
    public function getRows($map = [])
    {

        //3.30 删除 my_form  my_gift_id form_id 字段   数据库对应删除
        $rows = $this->alias('a')
            ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->where($map)
            ->field('s.skill_name,s.skill_id,a.apply_id,s.gift_id,s.spec,s.img,a.user_id')
            ->cache(15)
            ->select();

        foreach ($rows as $k => $v){

            $where['a.skill_id'] = $v['skill_id'];
            $where['a.user_id']  = $v['user_id'];
            $where['a.status']   = 1;
            $forms = Db::name('skill_form_user')->alias('a')
                ->join('skill_form s','s.form_id = a.form_id','LEFT')
                ->where($where)
                ->field('a.id,a.form_id,a.mini_price,a.my_gift_id,s.form_name')
                ->select();

            foreach ($forms as $key => $val){
                $gift['gift_id'] = ['in',$val['my_gift_id']];
                $gift['status']  = 1;
                $forms[$key]['gifts']  = Db::name('gift')
                    ->where($gift)
                    ->order('price')
                    ->cache(15)
                    ->field('gift_id,gift_name,thumbnail,img,price')
                    ->select();
            }

            $rows[$k]['forms'] = $forms;

            $rows[$k]['user_id'] = hashid($v['user_id']);

//            $myGift['gift_id'] = ['in',$v['my_gift_id']];
//            $myGift['status']  = 1;
//            $mini_price = Db::name('gift')->where($myGift)->order('price')->cache(15)->value('price');
            // 删除该字段 数据库同步删除
//            $rows[$k]['mini_price'] = getMiniPrice($v['my_gift_id']);




//                $form['form_id']  = ['in',$v['form_id']];


//            $form['status']   = 1;
//            $rows[$k]['form'] = Db::name('skill_form')->where($form)->field('form_id,form_name')->cache(10)->select();

//            $gift['gift_id']  = ['in',$v['gift_id']];


//            $gift['status']   = 1;
//            $rows[$k]['gift'] = Db::name('gift')->where($gift)->field('gift_id,gift_name,thumbnail,img,price')->order('price')->cache(10)->select();

        }

        return $rows;
    }



    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 用户筛选
     */
    public function getUsers($map = [],$order = '',$user_id)
    {

        $join = [
            ['user_extend e','e.user_id = a.user_id','left'],
            ['users u','u.user_id = a.user_id','left'],
            ['skill_form_user f','f.user_id = a.user_id','left'],
        ];
        //3.30 删除a.mini_price字段
        $field = 'f.id,e.place,a.skill_id,f.mini_price,a.num,u.user_id,u.header_img,u.sex,u.nick_name,a.explain as sign,e.online_time,e.online_status,e.room_id,e.noble_id,e.level,e.noble_time';

        $_empty = self::$_empty;
        $rows =  $this->alias('a')->where($map)->join($join)
            ->field($field)
            ->group('a.user_id')
            ->order($order)
            ->cache(5)
            ->paginate(12);


        $items = $rows->items();

//        $page = input('page')??1;
//        if ($page == 1){
//            $map['a.user_id'] = $user_id;
//            $self = $this->alias('a')->where($map)->join($join)
//                ->field($field)
//                ->cache(15)
//                ->find();
//            if ($self){
//                array_unshift($items,$self);
//            }
//        }

        foreach ($items as $k => $item){

            $items[$k]['header_img']   = getThumb($item['header_img'],200);

            if ($item['num'] < 5){
                $items[$k]['num'] = 5;
            }
            $item['distance'] = 0;

            if ($item['online_status']){

                $item['status'] = '当前在线';

            }else{

                $item['status'] = formatTime($item['online_time'],2);

            }
            $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
            $item['noble_name'] = getNobleName($item['noble_id']);
            if ($item['room_id']){

                //在房间中查询房间是否加密
                $item['skill'] = $_empty;
                $item['is_lock'] = \app\api\controller\Base::StaticRoomInfo($item['room_id'],'is_lock');
                $item['password'] = \app\api\controller\Base::StaticRoomInfo($item['room_id'],'password');
                $item['power']   = getPowerCode($item['user_id'],$item['room_id']);

            }else{

                $item['is_lock']  = 0;
                $item['password'] = '';
                $item['power']    = 4;

            }

            //用户不在房间中   查询是否有技能
            $skill['a.skill_id'] = $item['skill_id'];
            $skill['a.user_id']  = $item['user_id'];

            $item['skill'] = Db::name('skill_apply')
                    ->alias('a')
                    ->join( [
                        ['skill s','s.skill_id = a.skill_id','left']
                    ])
                    ->order('a.num desc')
                    ->where($skill)
                    ->field('a.apply_id,s.skill_name,a.explain')
                    ->find()??$_empty;

            $item['user_id'] = hashid($item['user_id']);
        }



//            ->each(function ($item)use ($_empty){
//
//                $item['distance'] = 0;
//
//                if ($item['online_status']){
//
//                    $item['status'] = '当前在线';
//
//                }else{
//
//                    $item['status'] = formatTime($item['online_time']);
//
//                }
//                $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
//                $item['noble_name'] = getNobleName($item['noble_id']);
//                if ($item['room_id']){
//                    //在房间中查询房间是否加密
//                    $item['skill'] = $_empty;
//                    $item['is_lock'] = \app\api\controller\Base::roomInfo($item['room_id'],'is_lock');
//                    $item['password'] = \app\api\controller\Base::roomInfo($item['room_id'],'password');
//                    $item['power']   = getPowerCode($item['user_id'],$item['room_id']);
//
//                }else{
//
//                    $item['is_lock']  = 0;
//                    $item['password'] = '';
//                    $item['power']    = 4;
//
//                    //用户不在房间中   查询是否有技能
//                    $skill['a.skill_id'] = $item['skill_id'];
//                    $skill['a.user_id']  = $item['user_id'];
//
//                    $item['skill'] = Db::name('skill_apply')
//                        ->alias('a')
//                        ->join( [
//                            ['skill s','s.skill_id = a.skill_id','left']
//                        ])
//                         ->order('a.num desc')
//                        ->where($skill)
//                        ->field('a.apply_id,s.skill_name')
//                        ->find()??$_empty;
//                }
//                $item['user_id'] = hashid($item['user_id']);
//            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$items];
    }




    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 同城查询
     */
    public function getCity($map = [],$userExtra = [] ,$distance = 5,$order = '')
    {

        // 3.30 删除a.mini_price字段
//        $_empty = self::$_empty;

        $join = [
            ['user_extend e','e.user_id = a.user_id','left'],
            ['users u','u.user_id = a.user_id','left'],
            ['skill_form_user f','f.user_id = a.user_id','left'],
        ];

        $log = $userExtra['log'];
        $lat = $userExtra['lat'];
        if (!$log){
            api_return(0,'无定位信息');
        }

        $field = "e.place,(st_distance (point (e.log,e.lat),point($log,$lat) ) / 0.0111) AS distance,a.skill_id,f.mini_price,a.num,u.user_id,u.header_img,u.sex,u.nick_name,a.explain as sign,e.online_time,e.online_status,e.room_id,e.noble_id,e.noble_time,e.level";

        $rows =  $this->alias('a')
            ->where($map)
            ->join($join)
            ->field($field)
            ->order($order)
            ->group('a.user_id')
            ->having("distance < $distance")
            ->limit(App::extend('cityNum'))
            ->select();
        foreach ($rows as $k => $item){
            $rows[$k]['header_img']   = getThumb($item['header_img'],200);

            if ($item['num'] < 5){
                $rows[$k]['num'] = 5;
            }

            if ($item['online_status']){
                $rows[$k]['status'] = '当前在线';
            }else{
                $rows[$k]['status'] = formatTime($item['online_time'],2);
            }
            $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
            $item['noble_name'] = getNobleName($item['noble_id']);
            if ($item['room_id']){
//                $rows[$k]['skill'] = $_empty;
                $item['is_lock'] = \app\api\controller\Base::roomInfo($item['room_id'],'is_lock');
                $item['password'] = \app\api\controller\Base::roomInfo($item['room_id'],'password');
                $item['power']   = getPowerCode($item['user_id'],$item['room_id']);
            }else{
                $item['is_lock']  = 0;
                $item['password'] = '';
                $item['power']    = 4;
            }

            $skill['a.skill_id'] = $item['skill_id'];
            $skill['a.user_id']  = $item['user_id'];

            $rows[$k]['skill'] = Db::name('skill_apply')
                ->alias('a')
                ->join([
                    ['skill s','s.skill_id = a.skill_id','left']
                ])
                ->order('a.num desc')
                ->where($skill)
                ->field('a.apply_id,s.skill_name,a.explain')
                ->find();
            $rows[$k]['user_id'] = hashid($item['user_id']);
        }
        return ['hasNext'=>false,'thisPage'=>$distance,'data'=>$rows];
    }


    public function getCity1($map = [],$userExtra = [] ,$distance = 5,$order = '')
    {

        // 3.30 删除a.mini_price字段
//        $_empty = self::$_empty;

        $join = [
            ['user_extend e','e.user_id = a.user_id'],
            ['users u','u.user_id = a.user_id'],
            ['skill_form_user f','f.user_id = a.user_id'],
        ];

        $log = $userExtra['log'];
        $lat = $userExtra['lat'];
        if (!$log){
            api_return(0,'无定位信息');
        }

        $field = "e.place,(st_distance (point (e.log,e.lat),point($log,$lat) ) / 0.0111) AS distance,a.skill_id,f.mini_price,a.num,u.user_id,u.header_img,u.sex,u.nick_name,a.explain as sign,e.online_time,e.online_status,e.room_id,e.noble_id,e.noble_time,e.level";

        $rows =  $this->alias('a')
            ->where($map)
            ->join($join)
            ->field($field)
            ->order($order)
            ->group('a.user_id')
            ->having("distance < $distance")
            ->select();
        foreach ($rows as $k => $item){

            if ($item['online_status']){
                $rows[$k]['status'] = '当前在线';
            }else{
                $rows[$k]['status'] = formatTime($item['online_time']);
            }
            $item['noble_id'] = \app\api\controller\Base::checkNoble($item);
            $item['noble_name'] = getNobleName($item['noble_id']);
            if ($item['room_id']){
//                $rows[$k]['skill'] = $_empty;
                $item['is_lock'] = \app\api\controller\Base::roomInfo($item['room_id'],'is_lock');
                $item['password'] = \app\api\controller\Base::roomInfo($item['room_id'],'password');
                $item['power']   = getPowerCode($item['user_id'],$item['room_id']);
            }else{
                $item['is_lock']  = 0;
                $item['password'] = '';
                $item['power']    = 4;
            }

            $skill['a.skill_id'] = $item['skill_id'];
            $skill['a.user_id']  = $item['user_id'];

            $rows[$k]['skill'] = Db::name('skill_apply')
                ->alias('a')
                ->join([
                    ['skill s','s.skill_id = a.skill_id','left']
                ])
                ->order('a.num desc')
                ->where($skill)
                ->field('a.apply_id,s.skill_name,a.explain')
                ->find();

            $rows[$k]['user_id'] = hashid($item['user_id']);
        }
        return ['hasNext'=>false,'thisPage'=>$distance,'data'=>$rows];
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 闪电邀约查询用户id
     */
    public function getFast($map = [],$field = 'a.user_id,s.skill_name,s.img,f.id')
    {

        $join = [
            ['user_extend e','e.user_id = a.user_id','left'],
            ['skill s','s.skill_id = a.skill_id','left'],
            ['skill_form_user f','f.user_id = a.user_id','left'],
        ];

        //存在性别筛选条件加入用户表筛选性别
        if (is_numeric($map['u.sex'])){
            $join[] =  ['users u','u.user_id = a.user_id','left'];
        }
        return $this->alias('a')->group('a.user_id')->join($join)->where($map)->field($field)->limit(30)->select();
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 闪电邀约查询用户信息
     */
    public function getFastInfo($map = [])
    {
        $field = 'f.form_id,f.form_name,a.apply_id,a.user_id,a.skill_id,s.skill_name,u.nick_name,u.header_img,a.num,u.sex,u.birthday,a.score,s.spec,a.explain,a.voice,e.place';

        $join = [
            ['user_extend e','e.user_id = a.user_id','left'],
            ['users u','u.user_id = a.user_id','left'],
            ['skill s','s.skill_id = a.skill_id','left'],
            ['skill_form f','f.skill_id = a.skill_id','left'],
//            ['skill_form_user fu','fu.skill_id = a.skill_id','left'],
        ];

        $rows =  $this->alias('a')->group('a.user_id')->join($join)->where($map)->field($field)->limit(30)->select();

        foreach ($rows as $k => $v){
            //3.30  修改miniprice

            $where['user_id']  = $v['user_id'];
            $where['form_id']  = $v['form_id'];
            $where['skill_id'] = $v['skill_id'];

            $rows[$k]['mini_price'] = Db::name('skill_form_user')->where($where)->value('mini_price');

            $rows[$k]['age'] = getAge($v['birthday']);
//            $rows[$k]['mini_price'] = getMiniPrice($v['my_gift_id']);
            $rows[$k]['user_id'] = hashid($v['user_id']);


        }

        return $rows;

    }
    



}
